package interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author fei
 * @date 2023/1/10 10:42
 * @description 当线程的中断标识为 true，是不是线程就立刻停止
 */
public class InterruptDemo2 {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i <= 300; i++) {
                System.out.println("-----: " + i);
            }
            System.out.println("t1 线程调用 interrupt() 后的中断标识02：" + Thread.currentThread().isInterrupted());
        }, "t1");
        t1.start();

        // false
        System.out.println("t1线程默认的中断标识：" + t1.isInterrupted());

        try{ TimeUnit.MILLISECONDS.sleep(2);} catch (InterruptedException e) { e.printStackTrace(); }
        t1.interrupt();
        // true
        System.out.println("t1 线程调用 interrupt() 后的中断标识01：" + t1.isInterrupted());

        // 睡眠 2000 毫秒，t1的线程早就执行完毕了
        try{ TimeUnit.MILLISECONDS.sleep(2000);} catch (InterruptedException e) { e.printStackTrace(); }
        t1.interrupt();
        // false，因为 interrupt 中断不活动的线程不会产生任何影响
        System.out.println("t1 线程调用 interrupt() 后的中断标识03：" + t1.isInterrupted());
    }

}
